import Listeners.TouchState
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Graphics
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration
import com.badlogic.gdx.files.FileHandle
import com.badlogic.gdx.graphics.GL10
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.g2d.TextureAtlas
import com.badlogic.gdx.graphics.g2d.TextureRegion
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator
import com.badlogic.gdx.scenes.scene2d.Actor
import com.badlogic.gdx.scenes.scene2d.InputEvent
import com.badlogic.gdx.scenes.scene2d.InputListener
import com.badlogic.gdx.scenes.scene2d.ui.Image


inline var Graphics.floatWidth: Float
    get() = this.width.toFloat()
    set(_) = Unit

inline var Graphics.floatHeight: Float
    get() = this.height.toFloat()
    set(_) = Unit

/**
 * 清屏函数
 */
inline fun clear(red: Int = 1, green: Int = 1, blue: Int = 1, alpha: Float = 1f)
{
    Gdx.gl.glClearColor(red.toFloat(), green.toFloat(), blue.toFloat(), alpha)
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT)
}

inline fun config(block: LwjglApplicationConfiguration.() -> Unit): LwjglApplicationConfiguration
{
    val config = LwjglApplicationConfiguration()
    block.invoke(config)
    return config
}

/**
 * 自动去除重复文字的生成 FreeTypeBitmapFontData 的方法
 */
inline fun FreeTypeFontGenerator.generateDataUnRepeat(size: Int, characters: String, flip: Boolean = false): FreeTypeFontGenerator.FreeTypeBitmapFontData
{
    val builder = StringBuilder()
    val newCharSets = "${FreeTypeFontGenerator.DEFAULT_CHARS}$characters"
    for (char in newCharSets)
    {
        if (char !in builder)
        {
            builder.append(char)
        }
    }
    return this.generateData(size, builder.toString(), flip)
}

/**
 * 快捷创建Image的方式
 */
inline fun image(handle: FileHandle): Image
{
    val texture = Texture(handle)
    return Image(texture)
}

/**
 * 快捷创建Image的方式
 */
inline fun image(path: String): Image
{
    val texture = Texture(Gdx.files.internal(path))
    return Image(texture)
}


/**
 * 快速创建Drawable的方式
 */
inline fun drawable(handle: FileHandle) = image(handle).drawable

inline fun drawable(path: String) = image(path).drawable

/**
 * 触摸事件
 */
inline fun <T : Actor> T.setOnTouchListener(crossinline block: T.(T, TouchState) -> Unit)
{
    this.addListener(object : InputListener()
    {
        override fun touchDown(event: InputEvent?, x: Float, y: Float, pointer: Int, button: Int): Boolean
        {
            block.invoke(this@setOnTouchListener, this@setOnTouchListener, TouchState.touchDown)
            return true
        }

        override fun touchUp(event: InputEvent?, x: Float, y: Float, pointer: Int, button: Int)
        {
            block.invoke(this@setOnTouchListener, this@setOnTouchListener, TouchState.touchUp)
        }

        override fun touchDragged(event: InputEvent?, x: Float, y: Float, pointer: Int)
        {
            block.invoke(this@setOnTouchListener, this@setOnTouchListener, TouchState.touchDragged)
        }

    })
}


fun <T : TextureRegion> T.flipX(): T
{
    this.flip(true, false)
    return this
}

fun <T : TextureRegion> T.flipY(): T
{
    this.flip(false, true)
    return this
}

fun TextureAtlas.AtlasRegion.copy(): TextureAtlas.AtlasRegion
{
    return TextureAtlas.AtlasRegion(this)
}


